package com.tnstart.android.control.light.fragments;

import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.tnstart.android.control.R;
import com.tnstart.android.control.light.bean.SampleItem;

public abstract class BaseListFragment extends ListFragment {

    private ArrayAdapter adapter = null;

    private int listLayoutId = R.layout.list;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(getListLayoutId(), null);
    }

    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initAdapter();
        setListAdapter(adapter);
        reloadData();
    }

    public void initAdapter() {
        adapter = adapterBuilder();
    }

    @Override
    public void onListItemClick(ListView lv, View v, int position, long id) {
        processSelection(lv, v, position, id);

    }

    public void reloadData() {
        clear();
        doReloadData();
    }

    public void clear() {
        if (adapter != null) {
            adapter.clear();
        }
    }

    public ArrayAdapter getAdapter() {
        return adapter;
    }

    public abstract ArrayAdapter adapterBuilder();

    public abstract void processSelection(ListView lv, View v, int position, long id);

    public abstract void doReloadData();

    public void setListLayoutId(int id) {
        listLayoutId = id;
    }

    public int getListLayoutId(){ return listLayoutId; }

}
